package com.wang.project.consumer.payment;

import cn.hutool.json.JSONUtil;
import com.wang.project.service.PaymentService;
import com.wang.project.model.entity.Payment;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * consumerGroup 配置暂时没什么用，但是必须要配置
 */
@Service
@Slf4j
@RocketMQMessageListener(
        topic = "payment_refreshKey_topic",
        consumerGroup = "payment-refreshKey-group"
)
public class paymentFreshKeyConsumer implements RocketMQListener<Payment> {


    @Resource
    private PaymentService paymentService;


    /**
     * 保存刷新key的支付信息【未支付情况的信息】
     *
     * @param payment
     */
    @Override
    public void onMessage(Payment payment) {
        log.info("paymentFreshKeyConsumer success payment: " + JSONUtil.toJsonStr(payment));
        //更改用户的支付态
        boolean save = paymentService.save(payment);
        if (!save) {
            log.info("paymentFreshKeyConsumer onMessage 保存刷新key的数据失败");
        }
        log.info("用户：{} 完成支付流程 end...", payment.getUserId());
    }
}
